package com.xiaoxi.open.design.behavioral.chainofresponsibilitypattern.service.password;

import com.xiaoxi.open.design.behavioral.chainofresponsibilitypattern.domain.password.CheckResult;
import org.springframework.stereotype.Component;

/**
 * @ClassName LengthCheck
 * @Description
 * @Author xiaoxi
 * @Date 2022/7/11 21:02
 * @Version 1.0
 */
@Component
public class LengthCheck extends AbstractCheckPassword {

    private Integer minNum = 2;

    private Integer maxNum = 20;

    public LengthCheck() {
    }

    public LengthCheck(Integer minNum, Integer maxNum) {

        if (minNum >= this.minNum && minNum <= this.maxNum) {
            this.minNum = minNum;
        }

        if (maxNum >= minNum && maxNum <= this.maxNum) {
            this.maxNum = maxNum;
        }

    }

    @Override
    public void doHandler(String password, CheckResult checkResult) throws Exception {

        if (password.length() <= minNum || password.length() >= maxNum) {
            checkResult.getErrorReason().add("密码长度应大于等于：" + minNum + ",小于等于：" + maxNum);
        }

    }
}
